import {$http} from "@/utils/request";

const successStatus = [200, 500]


$http.baseUrl = 'http://127.0.0.1:8000'// 本地

$http.beforeRequest = function (options) {
    console.log('beforeDestroy',options.isLoading,options)
    if (options.url.indexOf("auth") !== -1) {
        let token = uni.getStorageSync("token") || "";
        options.header = {
            "content-Type": "application/json",
            token: token,
        };
    }

    if (options.isLoading) {
        uni.showLoading({
            title: "加载中",
        });
    }
    uni.showNavigationBarLoading();// 显示导航条加载动画
}

$http.afterRequest = function (options) {
    console.log(options)
    uni.hideLoading();
    uni.hideNavigationBarLoading();// 隐藏导航条加载动画
    if (options.res.data.message === "身份验证失败") {
        uni.showToast({
            title: "身份验证失败",
            icon: "none",
        });
        uni.removeStorageSync("token");
        uni.removeStorageSync("userInfo");
        setTimeout(() => {
            uni.navigateTo({
                url: "/pages/login/index",
            });

        }, 1000)
        return;
    }


    // if (!successStatus.includes(options.res.data.status)) {
    //     uni.showToast({
    //         title: options.data.message,
    //         icon: "none",
    //     });
    //     return;
    // }
    console.log(options.res)

    if (options.isToast && options.res.data.status === 200) {
        uni.showToast({
            title: options.message||options.res.data.message,
            icon: "none",
            duration: 1500,
        });
    }
    if (options.res.data.status !== 200){
        // console.log('报错报错')
        uni.showToast({
            title: options.res.data.message || '服务器内部错误或或请求错误',
            icon: "none",
            duration: 1500,
        });
    }




}


export {$http}
